package kolekcje.powtorka.trzecia;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.TreeSet;

public class MusicBox {
	
	private class ArtistComparator implements Comparator<Song> {

		@Override
		public int compare(Song song1, Song song2) {
			return song1.getArtist().compareTo(song2.getArtist());
		}
		
	}

	private ArrayList<Song> songList = new ArrayList<Song>();
	private HashSet<Song> songSet = new HashSet<Song>();
	private TreeSet<Song> songTreeSet = new TreeSet<Song>();
	private TreeSet<Song> songTreeSetArtistComparator = new TreeSet<Song>(new ArtistComparator());

	public MusicBox(){
		this.makeMusicBox();
	}

	private void makeMusicBox() {
		File file = MusicBoxSetter.fileText;
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String s;
			while((s = br.readLine()) != null)
				addSong(s);
			br.close();
		} catch (FileNotFoundException e) {
			System.out.println("Pliku nie odnaleziono");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		songSet.addAll(songList);
		songTreeSet.addAll(songList);
		songTreeSetArtistComparator.addAll(songList);
	}

	private void addSong(String s) {
		String[] tempString = s.split("/");
		songList .add(new Song(tempString[0], tempString[1], tempString[2], tempString[3]));
	}

	public void showSongs() {
		for(Song e : songList)
			System.out.println(e.toString());
	}

	public void showUniqueSongs() {
		for(Song e : songSet)
			System.out.println(e.toString());
	}

	public void showUniqueSortedByArtistSongs() {
		for(Song e: songTreeSetArtistComparator)
			System.out.println(e.toString());
	}

	public void showSortedByTitleSongs() {
		Collections.sort(songList);
		for(Song e : songList)
			System.out.println(e.toString());;
	}

	public void showUniqueSortedByTitleSongs() {
		for(Song e: songTreeSet)
			System.out.println(e.toString());
	}

}
